package com.leetcode.algorithm.y22.m09.w2;

/**
 * 2383. 赢得比赛需要的最少训练时长
 * 
 * https://leetcode.cn/problems/minimum-hours-of-training-to-win-a-competition/
 * 
 * @author jie.deng
 *
 */
class Question2383Solution01 {
	
	public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {
		int minEnergy = 0;
		int minExperience = 0;
		int curEnergy = 0;
		int curExperience = 0;
		for (int i = 0; i < experience.length; i++) {
			if (curExperience < experience[i] + 1) {
				minExperience = Math.min(minExperience, curExperience - experience[i] - 1);
			}
			curEnergy -= energy[i];
			curExperience += experience[i];
			if (curEnergy - 1 < minEnergy) {
				minEnergy = curEnergy - 1;
			}
		}
		int hours = -minExperience >= initialExperience ? -minExperience - initialExperience : 0;
		hours += -minEnergy >= initialEnergy ? -minEnergy - initialEnergy : 0;
		return hours;
	}
    
}